//
//  YuLeViewController.swift
//  StudyDY
//
//  Created by yinchangming on 2018/7/9.
//  Copyright © 2018年 yinchangming. All rights reserved.
//

import UIKit
let kMeunsH:CGFloat=220
class YuLeViewController: BaseViewController {
    
    // 懒加载 网络请求
    private lazy var yuLeVM=YuLeViewModel()
    private lazy var collectionView={ () -> UICollectionView in
        //创建布局
        let layout=UICollectionViewFlowLayout()
        layout.itemSize=CGSize(width: kItemWidth, height:kItemHeight )
        layout.minimumLineSpacing=0
        layout.minimumInteritemSpacing=kItemMargin
        layout.sectionInset=UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)//设置间距
        //分组高度
        layout.headerReferenceSize=CGSize(width: kScreenW, height: kHeaderViewH)
        
        //创建 collectioncollection
        let collection=UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collection.register(UINib(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: kNomorlD)
        collection.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderID)
        collection.autoresizingMask=[.flexibleWidth,.flexibleHeight]
        collection.backgroundColor=UIColor.white
        collection.contentInset=UIEdgeInsets(top: kMeunsH, left: 0, bottom: 0, right: 0)
        collection.dataSource=self
        return collection
    }()
    // 头部菜单控件
    lazy var menusView={ () ->YuLeCatgaryView in
        let meun =   YuLeCatgaryView.creatVeiw()
        meun.frame=CGRect(x: 0, y: -kMeunsH, width: kScreenW, height: kMeunsH)
        return meun
        
    }()
    override func viewDidLoad() {
        contentView=collectionView
        updateUI()
            super.viewDidLoad()
    }
}
extension YuLeViewController{
    func updateUI(){
        view.addSubview(collectionView)
        collectionView.addSubview(menusView)
        requset()
    }
    func requset(){
        yuLeVM.requstData {
            self.finshRequst()
            self.collectionView.reloadData()
            self.yuLeVM.anchorCatgary.removeFirst()
            self.menusView.anchor=self.yuLeVM.anchorCatgary
        }
    }
}
extension YuLeViewController : UICollectionViewDataSource{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return yuLeVM.anchorCatgary.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kNomorlD, for: indexPath) as! CollectionNormalCell
        cell.room=yuLeVM.anchorCatgary[indexPath.section].roomListResult[indexPath.item]
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return yuLeVM.anchorCatgary[section].room_list?.count ?? 0
    }
    
    //分组头部
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header=collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderID, for: indexPath) as! CollectionHeaderView
        header.anchor=yuLeVM.anchorCatgary[indexPath.section]
        return header
    }
}

